/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _X509_H_
#define _X509_H_

#include <string.h>
#include <stdlib.h>
#include "ql_error_type.h"
#include "ql_type.h"
#include "log.h"

// ----------------------------------------------
// Error type definition
// ----------------------------------------------
#define X509_SUCCESS                     (ERR_X509_BASE + 0)
#define X509_ERR_BASE64_INVALID_CHAR    -(ERR_X509_BASE + 1)
#define X509_ERR_BASE64_BUF_TOO_SMALL   -(ERR_X509_BASE + 2)
#define X509_ERR_PARSE_NO_FIND_SYMBAL   -(ERR_X509_BASE + 3)
#define X509_ERR_PARSE_CURVE_MISMATCH   -(ERR_X509_BASE + 4)
#define X509_ERR_PARSE_INVALID_PARAM    -(ERR_X509_BASE + 5)

// ----------------------------------------------
// type definition
// ----------------------------------------------
// -- x509 item data structure --
typedef struct {
    ql_byte_t* title;
    ql_size_t  type;
    ql_size_t  len;
    ql_byte_t* data;
} x509_item_t;
// -- key data structure --
typedef struct {
    x509_item_t version;
    x509_item_t private_key;
    x509_item_t key_type;
    x509_item_t public_key;
} x509_key_t;

#ifdef __cplusplus
extern "C"
{
#endif

/*
Init the key object.
Inputs:
key - Object storing the key.
*/
ql_void_t keyInit(x509_key_t* key);

/*
Parse the key string.
Inputs:
    src_data - key data, string format.
Outputs:
    dst_key - Parse private key info.
    If the parsing is successful, it returns X509_SUCCESS, otherwise return an error value
 */
ql_err_t keyParse(x509_key_t* dst_key, ql_byte_t* src_data);

/*
Free the memory occupied by the key.
Inputs:
    key - Object storing the key.
 */
ql_void_t keyFree(x509_key_t* key);


#ifdef __cplusplus
}
#endif

#endif
